package com.starblink.search.result.ui.cell

import android.content.res.ColorStateList
import androidx.core.content.ContextCompat
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.user.UserRelationCodeEnum
import com.starblink.android.basic.modules.mine.MineManager
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.basic.ext.load
import com.starblink.rocketreserver.fragment.AccountF
import com.starblink.basic.style.R
import com.starblink.search.databinding.CellSearchUserBinding

class UserResultCell(binding: CellSearchUserBinding, private val onFollowClick: ((model: AccountF, position: Int)-> Unit)? = null) :
    BaseBindingVH<AccountF, CellSearchUserBinding>(binding) {

    init {
        binding.imgAvatar.enableCorner(1000)
    }


    override fun onBind(model: AccountF, position: Int) {
        itemView.setOnClickListener {
            LoginCheckInterceptor.check2Login(it.context) {
                if (model.relationCode == UserRelationCodeEnum.MYSELF.code) {
                    MineManager.navigationToMinePersonHome(context = itemView.context)
                } else {
                    MineManager.navigationToOtherUserPersonHome(context = itemView.context, model.id, accountDeleted = model.deleted, userName = model.userName)
                }
            }
        }

        val error = ContextCompat.getDrawable(itemView.context, R.mipmap.mine_default_avatar)
        binding.imgAvatar.load(model.headImage ?: "", placeholder = error, error = error)
        binding.tvName.text =
            if (model.deleted == true) string(R.string.account_deleted) else model.userName
        binding.tvDes.visibleOrGone(model.description?.isNotEmpty() == true) {
            text = model.description
        }

        val map = getAppraisalRelationMap(model.relationCode)
        binding.btnFollow.visibleOrGone((map["text"] as String?)?.isNotEmpty() == true) {
            text = map["text"] as String?
            setTextColor(map["textColor"] as Int)
            backgroundTintList = ColorStateList.valueOf(map["bgColor"] as Int)
            strokeColor = ColorStateList.valueOf(map["lineColor"] as Int)
            setOnClickListener {
                onFollowClick?.invoke(model, position)
            }
        }

    }


    private fun getAppraisalRelationMap(v: Int): Map<*, *> {
        // # 与查看人的关系。-1 myself; 0无关系; 1已关注; 2粉丝; 3已互关
        val result = hashMapOf<String, Any>()
        when (v) {
            UserRelationCodeEnum.MYSELF.code -> {
                result["text"] = ""
                result["textColor"] = color(R.color.sk_gray0)
                result["lineColor"] = color(R.color.transparent)
                result["bgColor"] = color(R.color.sk_gray5)
            }
            UserRelationCodeEnum.NOTHING.code, UserRelationCodeEnum.FANS.code -> {
                result["text"] = "+ ${string(R.string.follow)}"
                result["textColor"] = color(R.color.sk_gray0)
                result["lineColor"] = color(R.color.sk_blue600)
                result["bgColor"] = color(R.color.sk_blue600)
            }
            UserRelationCodeEnum.FOLLOW.code, UserRelationCodeEnum.BOTH.code -> {
                result["text"] = string(R.string.following)
                result["textColor"] = color(R.color.sk_gray800)
                result["lineColor"] = color(R.color.sk_gray200)
                result["bgColor"] = color(R.color.transparent)
            }
        }
        return result
    }
}